package com.xinghou.platform.lucene.main.impl;

import com.xinghou.platform.dao.main.FavoriteDAO;
import com.xinghou.platform.dao.main.SharedCardDAO;
import com.xinghou.platform.entity.resp.ShareInfoResp;
import com.xinghou.platform.model.main.FavoriteCard;
import com.xinghou.platform.model.main.SharedCard;
import com.xinghou.platform.model.main.Subject;
import com.xinghou.platform.server.api.bussiness.FavoriteCardService;
import com.xinghou.platform.server.api.bussiness.ShareInfoBusinessService;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 16/6/23.
 */
@Service("favoriteCardService")
public class FavoriteCardServiceImpl implements FavoriteCardService {
    @Resource
    FavoriteDAO favoriteCardDAO;
    @Resource
    SharedCardDAO sharedCardDAO;

    @Resource
    ShareInfoBusinessService shareInfoBusinessService;

    @Override
    public void add(FavoriteCard favoriteCard) {
        int n=favoriteCardDAO.hasFavorityCard(favoriteCard.getUid(),favoriteCard.getCardid());
        if(n>0)
            throw new ServiceException(DBExceptionEnums.HAS_FAVORITE_CARD);
        favoriteCard.createtime=System.currentTimeMillis();
        favoriteCardDAO.addFavoriteCard(favoriteCard);

    }

    @Override
    public List<ShareInfoResp> getFavoriteCards( Long uid, Integer pageno, Integer pagesize) {
        List<SharedCard> sharedCards=new ArrayList<SharedCard>();
        List<ShareInfoResp> resps=new ArrayList<>();
        Integer startno=(pageno-1)*pagesize;
        List<Long> cardids = favoriteCardDAO.getFavoriteCards(uid, startno, pagesize);
        for(int i=0;i<cardids.size();i++)
        {
            SharedCard sharedCard=sharedCardDAO.getSharedCardBycardId(Integer.parseInt(cardids.get(i).toString()));
            shareInfoBusinessService.commSharedNew(sharedCard, resps);
        }
        return resps;
    }

    @Override
    public void del(Long uid,Long cardid) {
        favoriteCardDAO.delFavoriteCard(uid,cardid);
    }
}